package com.topscomm.mis.cbb.pm.controller;

import com.topscomm.cbo.pojo.CboApprovalNodeEntity;
import com.topscomm.cbo.service.impl.CboApprovalNodeService;
import com.topscomm.mis.cbb.pm.api.IFlowSkipService;
import com.topscomm.mis.cbb.pm.api.IWbsService;
import com.topscomm.mis.cbb.pm.base.pojo.CbbPmFlowSkipEntity;
import com.topscomm.mis.cbb.pm.base.pojo.CbbPmWbsEntity;
import com.topscomm.mis.cbb.pm.base.service.ICbbPmWbsService;
import com.topscomm.mis.cbb.pm.base.service.impl.CbbPmFlowSkipService;
import com.topscomm.mis.cbb.pm.controller.auto.CbbPmWbsControllerAuto;
import com.topscomm.pub.util.BaseUtil;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.pub.vo.ResponseResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

//import com.topscomm.mis.cbb.pm.util.TaskTreeModel;

@RestController
@RequestMapping("cbb/pmFlowSkip")
public class CbbPmFlowSkipController extends CbbPmWbsControllerAuto {
    @Autowired
    private CboApprovalNodeService cboApprovalNodeService;
    @Autowired
    private CbbPmFlowSkipService cbbPmFlowSkipService;
    @Autowired
    private IFlowSkipService flowSkipService;
    @Autowired
    private ICbbPmWbsService cbbPmWbsService;
    /***
     * @description: 查询当前流程可以跳过的节点
     * @param paraMap
     * @return com.topscomm.pub.vo.ResponseResult<java.lang.Object>
     * @author: wangxinhua
     * @date: 2023/6/26 17:21
     * @modify:
     */
    @GetMapping(value = "/queryFlowSkip")
    public ResponseResult<Object> queryFlowSkip(@RequestParam(required = false) Map<String, Object> paraMap) {
        paraMap = BaseUtil.decodeSecureMap(paraMap);
        String projectuucode = ConvertUtil.convertToString(paraMap.get("projectuucode"));
        CbbPmWbsEntity wbsEntity = cbbPmWbsService.queryObjectByWhere(" projectuucode='"+projectuucode+"'");
        String flowId = ConvertUtil.convertToString(wbsEntity.getFlowid());
//        paraMap.put("whereSql", "flowid='" + flowId + "' and approvalparam like '%nodeskipflag%' and nodetype in (1,2,3,4,5) and enabled=1 order by sortcode,id");
        paraMap.put("whereSql", "flowid='" + flowId + "'  and nodetype in (1,2,3,4,5) and enabled=1 order by sortcode,id");
        List<Map<String, Object>> list = cboApprovalNodeService.queryMap(paraMap, "com.topscomm.mis.cbb.pm.base.pojo.CbbPmFlowSkipEntity.queryFlowNodeWithTask");
        List<CbbPmFlowSkipEntity> insList = cbbPmFlowSkipService.queryByWhere("projectuucode='" + projectuucode + "'");
        for (Map<String, Object> map : list) {
            if (insList.stream().anyMatch(m -> m.getNodeid().equals(ConvertUtil.convertToString(map.get("id"))))) {
                map.put("skiped", true);
            } else {
                map.put("skiped", false);
            }
        }
        // 去除有执行中任务的节点
        list = list.stream().filter(item -> ConvertUtil.convertToBoolean(item.get("skiped")) || ConvertUtil.convertToInt(item.get("executenum")) <= 0).collect(Collectors.toList());
        return ResponseResult.ok(list);
    }

    /**
     * @description:保存数据
     * @param paraMap
     * @return
     * @author: zhanglongfei
     * @date: 2021年2月26日下午5:47:43
     * @modify:
     */
    @PostMapping(value = "/saveData")
    public ResponseResult<Object> saveData(@RequestBody(required = false) Map<String, Object> paraMap) {
        paraMap = BaseUtil.decodeSecureMap(paraMap);
        flowSkipService.saveData(paraMap);
        return ResponseResult.ok("保存成功");
    }
    private static final Logger logger = LoggerFactory.getLogger(CbbPmFlowSkipController.class);

    @GetMapping("/thread")
    public String testThread() {
        logger.info("start controller");
        flowSkipService.testThread();
        logger.info("end controller");
        return "";
    }
}
